<?php
namespace Game\Controller;
use Common\Controller\CommonController;
class TicketController extends CommonController{

    const AppID     =   'wx0dcb83c5892f258b';
    const AppSecret =   'aab64931415e6bb57986cb832518fc98';

    /**
     * @param $game_id
     * @param $uid
     * @param $appid
     */
    public function take()
    {
        $game_id    =   I('post.game_id','','htmlspecialchars');
        $uid        =   I('post.uid','','htmlspecialchars');
        $appid      =   I('post.appid','','htmlspecialchars');

        if (! empty($game_id) && ! empty($uid) && ! empty($appid))
        {
            $where          =   array();
            $where['appid'] =   $appid;
            $where['uid']   =   $uid;
            $field          =   array('uid','session_key','openid');
            $member_data    =   M('game_user')->where($where)->field($field)->find();

            if (empty($member_data))
            {
                $this->returnData('',$msg='用户不存在',$code=400);
            }

            $where              =   array();
            $where['appid']     =   $appid;
            $where['uid']       =   $uid;
            $where['game_id']   =   $game_id;
            $where['timestamp'] =   strtotime(date('Y-m-d',time()));
            $field              =   array('ticket');
            $result             =   M('game_user_ticket')->where($where)->field($field)->find();
            $data               =   array();
            if (! empty($result))
            {
                if ($result['ticket'] == 0)
                {
                    $this->returnData($data,$msg='入场卷不足',$code=-4);
                }
                else
                {
                    $update_data                =   array();
                    $update_data['last_signin'] =   time();
                    $update_data['ticket']      =   $result['ticket']   -   1;
                    M('game_user_ticket')->where($where)->save($update_data);
                    $data['ticket'] =   $result['ticket']   -   1;
                    $this->returnData($data,$msg='OK',$code=200);
                }
            }
            else
            {
                $insert_data                =   array();
                $insert_data['uid']         =   $uid;
                $insert_data['appid']       =   $appid;
                $insert_data['game_id']     =   $game_id;
                $insert_data['ticket']      =   2;      //默认机会是3 现在默认已经减去1
                $insert_data['timestamp']   =   strtotime(date('Y-m-d',time()));
                $insert_data['last_signin'] =   time();
                M('game_user_ticket')->add($insert_data);
                $data['ticket'] =   $insert_data['ticket'];
                $this->returnData($data,$msg='OK',$code=200);
            }

        }
    }

    /**
     * @param $game_id
     * @param $uid
     * @param $appid
     * @param $encrypted_data
     * @param $iv
     */
    public function add()
    {
        $game_id            =   I('post.game_id','','htmlspecialchars');
        $uid                =   I('post.uid','','htmlspecialchars');
        $appid              =   I('post.appid','','htmlspecialchars');
        $encrypted_data     =   I('post.encrypted_data','','htmlspecialchars');
        $iv                 =   I('post.iv','','htmlspecialchars');

        if (! empty($game_id) && ! empty($uid) && ! empty($iv)
        && ! empty($appid) && ! empty($encrypted_data))
        {
            $where          =   array();
            $where['appid'] =   $appid;
            $where['uid']   =   $uid;
            $field          =   array('uid','session_key','openid');
            $member_data    =   M('game_user')->where($where)->field($field)->find();

            if (empty($member_data))
            {
                $this->returnData('',$msg='用户不存在',$code=400);
            }

            //解析openGId
            $openGId = $this->getopenGId($appid, $member_data['session_key'], $encrypted_data, $iv);

            $now  = date('Y-m-d',time());
            if ($openGId != '-1')
            {
                $where_group['sharetime']   = $now;
                $where_group['uid']         = $uid;
                $where_group['opengid']     = $openGId;
                $where_group['game_id']     = $game_id;

                $record = M('game_wechat_group')->where($where_group)->getField('id'); //判断是否已分享
                if ($record)
                {
                    M('game_wechat_group')->where($where_group)->setInc('sharenum');
                    $this->returnData('','今天已分享',-8);
                }
                else
                {
                    $insert_data                =   array();
                    $insert_data['opengid']     =   $openGId;
                    $insert_data['sharetime']   =   $now;
                    $insert_data['sharenum']    =   1;
                    $insert_data['uid']         =   $uid;
                    $insert_data['create_time'] =   time();
                    $insert_data['game_id']     =   $game_id;
                    M('game_wechat_group')->add($insert_data);

                    $where_ticket              =   array();
                    $where_ticket['appid']     =   $appid;
                    $where_ticket['uid']       =   $uid;
                    $where_ticket['game_id']   =   $game_id;
                    $where_ticket['timestamp'] =   strtotime(date('Y-m-d',time()));
                    $field                     =   array('ticket');
                    $ticket_data               =   M('game_user_ticket')->where($where_ticket)->field($field)->find();

                    //增加机会
                    if (!empty($ticket_data))
                    {
                        $update_data           =    array();
                        $update_data['ticket'] =    $ticket_data['ticket']  +   1;
                        M('game_user_ticket')->where($where_ticket)->save($update_data);
                        $data['ticket'] =   $update_data['ticket'];
                        $this->returnData($data,$msg='OK',$code=200);
                    }
                    else
                    {
                        $insert_data                =   array();
                        $insert_data['appid']       =   $appid;
                        $insert_data['game_id']     =   $game_id;
                        $insert_data['ticket']      =   3;
                        $insert_data['timestamp']   =   time();
                        $insert_data['last_signin'] =   time();
                        M('game_user_ticket')->add($insert_data);
                        $data['ticket'] =   $insert_data['ticket'];
                        $this->returnData($data,$msg='OK',$code=200);
                    }
                }
            }
            else
            {
                $this->returnData('',$msg='分享失败',$code=400);
            }

        }

    }
}